/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.escom.healthlc.ws;

import com.escom.healthlc.business.ConsultaBusiness;
import com.escom.healthlc.entidad.ConsultaVO;
import com.escom.healthlc.entidad.EstudioORM;
import com.escom.healthlc.utils.Constantes;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.*;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import org.springframework.core.io.ClassPathResource;

/**
 *
 * @author Carlos
 */
public class FileSocketService {

    private ApplicationContext appContext;
    private static FileSocketService fileSocketService;

    private FileSocketService() {
        appContext = new FileSystemXmlApplicationContext(Constantes.PATH_APP_CONTEXT);
    }

    public static FileSocketService getInstance() {
        if (fileSocketService == null) {
            fileSocketService = new FileSocketService();
        }
        return fileSocketService;
    }

    public void initProcess() {
        ServerSocket serverSocket = null;
        try {
            serverSocket = new ServerSocket(Constantes.PORT_FILE);
        } catch (IOException ex) {
            Logger.getLogger(FileSocketService.class.getName()).log(Level.SEVERE, null, ex);
        }
        while (true && serverSocket != null) {
            System.out.println("Esperando Conexion");
            Socket socket;
            try {
                socket = serverSocket.accept();

                InputStream inputStream = socket.getInputStream();
//                InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
//                BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
//                String validation = bufferedReader.readLine();
//                if (validation.equals("START-FILE")) {
                ObjectInputStream objectInputStream = new ObjectInputStream(inputStream);
                EstudioORM estudioORM = (EstudioORM) objectInputStream.readObject();

                String idMedico = estudioORM.getIdMedico();
                String idPaciente = estudioORM.getIdPaciente();
                String path = Constantes.PATH_WEB_VIRTUAL;
                String fileName = new SimpleDateFormat("yyyy_MM_dd_HH_mm_ss").format(Calendar.getInstance().getTime())
                        + "-" + idMedico;
                File fileObject = new File(path + fileName + ".txt");
                ConsultaVO consultaVO = new ConsultaVO();
                consultaVO.setIdMedico(idMedico);
                consultaVO.setIdPaciente(idPaciente);
                consultaVO.setArchivoPrueba(fileName);

                fileObject.createNewFile();
                ConsultaBusiness consultaBusiness = appContext.getBean(ConsultaBusiness.class);
                consultaBusiness.actualizarArchivoPrueba(consultaVO);
                ObjectOutputStream os = new ObjectOutputStream(new FileOutputStream(fileObject));
                os.writeObject(estudioORM);
                os.close();
                socket.close();
//                }
            } catch (IOException ex) {
                Logger.getLogger(FileSocketService.class.getName()).log(Level.SEVERE, null, ex);
            } catch (ClassNotFoundException ex) {
                Logger.getLogger(FileSocketService.class.getName()).log(Level.SEVERE, null, ex);
            }

        }

    }

}
